<template>
  <div>
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="onSubmit">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDetailByID } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    userId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 存到已选角色的id
      roleList: []
    }
  },
  created() {
    this.getRoles()
    this.getUserRoles()
  },
  methods: {
    // 确认按钮
    async onSubmit() {
      const res = await assignRoles({ id: this.userId, roleIds: this.roleIds })
      // console.log('666666', res)
      this.$message.success(res.message)
      this.$emit('close')
    },
    // 获取员工基本信息
    async getUserRoles() {
      const res = await getUserDetailByID(this.userId)
      // console.log('555555', res)
      this.roleIds = res.data.roleIds === null ? [] : res.data.roleIds
    },
    // 获取所有的角色列表
    async getRoles() {
      const res = await getRoles()
      this.roleList = res.data.rows
    },
    // 取消按钮
    cancleDialog() {
      this.$emit('close')
    }
  }

}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
